/**
 * 
 */
package au.com.langdale.gwt.client.binding;

import au.com.langdale.gwt.client.binding.BindingManager.Binding;
import au.com.langdale.gwt.client.model.Property;

import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Widget;

/**
 * Binds a CheckBox to a model and property.
 * 
 * @author adv
 *
 */
public class CheckBoxBinding extends Binding implements ClickListener {

	private CheckBox checkBox;

	public CheckBoxBinding(Object model, Property prop, CheckBox box) {
		super(model, prop);
		checkBox = box;
		checkBox.addClickListener(this);
		refresh();
	}
	
	public void refresh() {
		checkBox.setChecked(prop.isSet(model));
	}

	public void onClick(Widget sender) {
		prop.set(model, checkBox.isChecked());
		fireChange();
	}
	
	public void destroy() {
		if( checkBox != null) {
			checkBox.removeClickListener(this);
			checkBox = null;
		}
	}

	public Widget getWidget() {
		return checkBox;
	}
}